package gms.actions.match;

import gms.actions.SuperAction;
import gms.entities.field.Field;
import gms.entities.match.MatchApply;
import gms.service.field.FieldService;
import gms.service.match.MatchApplyService;
import gms.util.PageBean;

import java.io.IOException;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class MatchApplyAction extends SuperAction implements ModelDriven<MatchApply> {

	private MatchApply matchApply = new MatchApply();  //赛事申请表对象
	
	@Override
	public MatchApply getModel() {
		return matchApply;
	}

	public MatchApply getMatchApply() {
		return matchApply;
	}

	public void setMatchApply(MatchApply matchApply) {
		this.matchApply = matchApply;
	}

	private int currPage;  //当前页面显示的页数
	
	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
	
	private String idList; //赛事申请表id集合
	
	public String getIdList() {
		return idList;
	}

	public void setIdList(String idList) {
		this.idList = idList;
	}

	private MatchApplyService matchApplyService;  //赛事申请表服务对象

	public void setMatchApplyService(MatchApplyService matchApplyService) {
		this.matchApplyService = matchApplyService;
	}
	
	private FieldService fieldService;  //场地模块服务
	
	public void setFieldService(FieldService fieldService) {
		this.fieldService = fieldService;
	}

	/**
	 * 保存赛事申请表，成功返回查看赛事申请表界面
	 * @return
	 */
	public String save(){
		matchApply.setAuditStatus("申请成功");
		boolean result = matchApplyService.save(matchApply);
		if(result == true){
			System.out.println("保存成功");
			return "saveSuccess";
		}else{
			System.out.println("保存失败");
		}
		return "saveSuccess";
	}
	
	/**
	 * 申请赛事，返回申请赛事界面
	 * @return
	 */
	public String apply(){
		return "apply";
	}
	
	
	/**
	 * 分页查询赛事申请表
	 * @return
	 */
	public String findAll(){
		PageBean<MatchApply> pageBean = matchApplyService.findByPage(currPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "findAll";
	}
	
	/**
	 * 审核赛事申请表，返回审核赛事申请表界面
	 * @return
	 */
	public String examine(){
		List<MatchApply> list = matchApplyService.examine();
		PageBean<MatchApply> pageBean = matchApplyService.listPage(currPage,list);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "examine";
	}
	
	/**
	 * 审核通过，更新审核状态，添加赛事申请表到赛事信息表
	 * @return
	 */
	public String exPassed(){
		matchApplyService.exPassed(matchApply);
		matchApplyService.toMatchInfo(matchApply);
		System.out.println("审核通过");
		Field field = fieldService.findFieldById(matchApply.getPlaceID());
		field.setField_mark(1);
		fieldService.updateField(field);
		return "exSuccess";
	}
	
	/**
	 * 审核成功，更新审核状态，添加赛事申请表到赛事信息表
	 * @return
	 */
	public String exFailed(){
		matchApplyService.exFailed(matchApply);
		System.out.println("审核不通过");
		return "exSuccess";
	}
	
	/**
	 * 删除赛事申请表，返回删除界面
	 * @return
	 */
	public String delete(){
		PageBean<MatchApply> pageBean = matchApplyService.findByPage(currPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "delete";
	}
	
	/**
	 * 批量删除选中的赛事申请表，删除成功返回原界面
	 * @return
	 */
	public String deleteAll(){
		System.out.println(idList);
		String[] l = idList.split(",");
		for (String id : l) {
			matchApply = matchApplyService.findByID(Integer.parseInt(id));
			matchApplyService.delete(matchApply);
		}
		System.out.println("deleteAll success");
		return "deleteSuccess";
	}
	
	/**
	 * 调用场地模块服务，查找符合赛事类型并且没有被租借的场地ID
	 */
	public void findFieldByTypeAndNoRent(){
		String ft = request.getParameter("field_type");
		System.out.println(ft);
		String idList = fieldService.findFieldByTypeAndNoRent(ft);
		System.out.println(idList);
		try {
			response.getWriter().print(idList);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
